package org.abluedog.ssfb.listeners;

import org.abluedog.ssfb.StartSurvivalFromBaby;
import org.abluedog.ssfb.events.EatEvent;
import org.abluedog.ssfb.pdc.BodyComposition;
import org.abluedog.ssfb.pdc.BodyCompositionPDC;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.plugin.Plugin;

public class SSFBListener implements Listener {

    @EventHandler()
    public void onPlayerDeath(PlayerDeathEvent event) {
        Plugin plugin = StartSurvivalFromBaby.getPlugin();
        if (!plugin.getConfig().getBoolean("death-protection")) {
            //todo clear growth information
            Player entity = event.getEntity();
            NamespacedKey growth = new NamespacedKey(plugin, "player-body-composition-" + entity.getUniqueId());
            entity.getPersistentDataContainer().set(growth, new BodyCompositionPDC(), new BodyComposition());
        }
    }

    @EventHandler()
    public void consumeItem(PlayerItemConsumeEvent event) {
        Plugin plugin = StartSurvivalFromBaby.getPlugin();
        if (event.getItem().getType().isEdible()) {
            Material type = event.getItem().getType();
            Material material = Material.getMaterial(type.name());
            plugin.getServer().getPluginManager().callEvent(new EatEvent(event.getPlayer(), material));
        }
    }
}
